﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE80;
using EnvDTE;
using QuickOpen.Controls;
using QuickOpen.Framework.Repositories;
using QuickOpen.Framework;

namespace QuickOpen
{
    public partial class MainForm : Form
    {
        public MainForm(DTE2 applicationObject, Settings settings)
        {
            InitializeComponent();
            _settings = settings;
            _applicationObject = applicationObject;
        }

        protected override void OnLoad(EventArgs e)
        {
            this.Size = _settings.FormSize;
            //Right now we only have the file repository.
            //In the future we may have a repository for specfic types like
            //ImageRepository or ReferenceRepository
            _filesListBox.LoadRepository(new FileRepository(_applicationObject));
            base.OnLoad(e);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            _settings.FormSize = this.Size;
            base.OnClosing(e);
        }

        //Global form hook because PreviewKey is on.
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    _filesListBox.MoveUp();
                    e.Handled = true;
                    break;
                case Keys.Down:
                    _filesListBox.MoveDown();
                    e.Handled = true;
                    break;
                case Keys.Back:
                    _filterTextBox.Backspace();
                    break;
                case Keys.Enter:
                    if (_filesListBox.PerformAction())
                        Close();
                    e.Handled = true;
                    break;
                case Keys.Escape:
                    Close();
                    e.Handled = true;
                    break;
            }

            base.OnKeyDown(e);
        }

        private void _filterTextBox_TextChanged(object sender, EventArgs e)
        {
            _filesListBox.ApplyFilter(_filterTextBox.Text);

            if (_filesListBox.Items.Count > 0)
            {
                IFilterableItem item = _filesListBox.Items[0] as IFilterableItem;
                _filterTextBox.AutoComplete(item);
                _statusLabel.Text = item.LongName;
            }
            else
            {
                _statusLabel.Text = "Ready";
            }
        }

        private void _filesListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _statusLabel.Text = (_filesListBox.SelectedItem as IFilterableItem).LongName;
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            //Visible = false;
            using (AboutForm af = new AboutForm())
                af.ShowDialog();
            //Visible = true;
        }

        private Settings _settings;
        private DTE2 _applicationObject;
    }
}
